﻿using UnityEngine;
using System.Collections;

/*Comment
 * 队形文件，队形包含了队伍如何移动，如何分配每个小兵的位置
 * 基本的信息采用组合的方式构成
 * 这个脚本需要挂在到Square上
 */

public enum ESquareState
{
	emLive,
	emDead,
}

public class Square : MonoBehaviour {

	bool bSelect;

	public bool IsSelect
	{
		set {bSelect = value;}
		get {return bSelect;}
	}

	protected ESquareState eState;

	public MoveHelper control;
	public Soldier leader;
	public Soldier[] soleiers;
	public SquareMove sqrMove;

	public Body body;
	public Collider AttackRange;

	public Vector3 VProjectPosition
	{
		get{
			Vector3 vec = transform.position;
			vec.y = 0;
			
			return vec;
		}
		
		set{
			Vector3 vec = value;
			vec.y = transform.position.y;
			
			transform.position = vec;
		}
	}
	
	// Use this for initialization
	void Start () {
		if (null != control) {
			control.selSquare += SelectSquare;
			control.pickTarget += MoveTo;
		}

		leader.evtLeadEvent += OnLeaderAction;
		body.evtDeath += OnDeath;

		eState = ESquareState.emLive;

		InitInheritSquare ();
	}

	protected virtual void InitInheritSquare()
	{

	}

	void SelectSquare (object sender, SelectSquareArgs e)
	{
		Square sq = sender as Square;

		if (sq.gameObject.Equals(this.gameObject))
			bSelect = e.bSelect;
	}
	
	// Update is called once per frame
	void Update () {

	}

	public void MoveTo(object sender, PickTargetArgs args){
		if (args.hitObj.tag == "Ground") {
			sqrMove.SetDestination (args.position);
		}

	}
	
	void OnLeaderAction (object sender, LeaderMsg evt){
		if (evt.eAction == ELeaderAction.emAttack) {
			DoAttackAction();
		}
	}

	void DoAttackAction(){
		Debug.Log ("do attack at time:" + Time.time);

		GameObject meleeAttack = Instantiate(Resources.Load("Other/DamageType/Base/MeleeLancer")) as GameObject;
		
		meleeAttack.GetComponent<HitAction>().Creator = this;
	}

	void OnDeath (object obj, DeathEvent evt) {
		gameObject.SetActive (false);
		leader.Retreat ();
	}
	
	public Vector3 GetVProjectPosition()
	{
		Vector3 vec = transform.position;
		vec.y = 0;
		
		return vec;
	}

	/*public virtual void OnHit(object sender, HitEvent evt) {

	}

	public virtual void OnHurt(object sender, HitEvent evt) {

	}*/

}
